package com.hqyj.freemovie.front.satoken;

import cn.dev33.satoken.stp.StpInterface;
import com.hqyj.freemovie.front.domain.Roles;
import com.hqyj.freemovie.front.service.UsersPermissionService;
import com.hqyj.freemovie.front.service.UsersRolesService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 自定义权限验证接口扩展
 */
@Component    // 保证此类被SpringBoot扫描，完成Sa-Token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private UsersRolesService usersRolesService;

    @Resource
    private UsersPermissionService usersPermissionService;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 本list仅做模拟，实际项目中要根据具体业务逻辑来查询权限
        List<String> list = new ArrayList<String>();
        list = usersPermissionService.queryUserPermissionsByUid(Integer.parseInt(loginId.toString()));
        return list;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 本list仅做模拟，实际项目中要根据具体业务逻辑来查询角色
        List<Roles> rolesList = usersRolesService.queryUsersRolesByUid(Integer.parseInt(loginId.toString()));
        List<String> list = new ArrayList<String>();
        if(rolesList!=null && rolesList.size()>0){
            for(Roles r : rolesList){
                list.add(r.getRname());
            }
        }

        return list;
    }

}

